package com.wandian.util
{
	import com.greensock.TweenLite;
	import com.wandian.ApplicationFacade;

	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;

	/**
	 * 定时器帮助类
	 * @author long
	 */
	public class CLZTiming
	{
		private static var _timer:Timer;
		private static var _timerFun:Timer;
		private static var facade:ApplicationFacade=ApplicationFacade.getInstance();
		private static const DICT:Dictionary=new Dictionary();

		public function CLZTiming()
		{
			return;
		} 

		/**
		 * 按时间缓动
		 * @param duration
		 * @param fun
		 * @param args
		 */
		public static function add(duration:Number, fun:Function, ... args):void
		{
			if (DICT[fun])
			{
				DICT[fun].kill();
			}
			DICT[fun]=TweenLite.to(CLZTiming, duration, {onComplete: complete, onCompleteParams: [fun, args]});
			return;
		} 

		/**
		 * 按帧缓动
		 * @param duration
		 * @param fun
		 * @param args
		 */
		public static function addByFrames(duration:Number, fun:Function, ... args):void
		{
			if (DICT[fun])
			{
				DICT[fun].kill();
			}
			DICT[fun]=TweenLite.to(CLZTiming, duration, {onComplete: complete, useFrames: true, onCompleteParams: [fun, args]});
			return;
		} 

		/**
		 * 移除回调字典
		 * @param fun
		 */
		public static function remove(fun:Function):void
		{
			if (DICT[fun])
			{
				DICT[fun].kill();
				deleteFunc(fun);
			}
			return;
		} 

		/**
		 * 定时到，执行函数
		 * @param fun
		 * @param argArray
		 */
		private static function complete(fun:Function, argArray:Array):void
		{
			deleteFunc(fun);
			fun.apply(null, argArray);
			return;
		} 

		private static function deleteFunc(fun:Function):void
		{
			delete DICT[fun];
			return;
		} 

		/**
		 * 定时发送消息
		 * @param interval
		 * @param noticeName
		 * @param obj
		 */
		public static function setTimingNotice(interval:int, noticeName:String, obj:Object):void
		{
			var timerComplete:Function;
			var time:*=interval;
			timerComplete=function(event:TimerEvent):void
			{
				facade.sendNotification(noticeName, obj);
				return;
			} 
			;
			if (_timer != null)
			{
				_timer.reset();
				_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
				_timer=null;
			}
			_timer=new Timer(time, 1);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
			_timer.start();
			return;
		} 

		public static function setTimingTip(time:uint=0, fun:Function=null):void
		{
			var timerFunComplete:Function;
			timerFunComplete=function(event:TimerEvent):void
			{
				if (fun != null)
				{
					fun();
				}
				return;
			} 
			;
			if (_timerFun != null)
			{
				_timerFun.reset();
				_timerFun.removeEventListener(TimerEvent.TIMER_COMPLETE, timerFunComplete);
				_timerFun=null;
			}
			if (time == 0)
			{
				if (fun != null)
				{
					fun();
				}
				return;
			}
			_timerFun=new Timer(time, 1);
			_timerFun.addEventListener(TimerEvent.TIMER_COMPLETE, timerFunComplete);
			_timerFun.start();
			return;
		} 

	}
}
